from flask import Flask, request, jsonify
import json
import requests
from once_chat import IndustryAI
import base64
import cv2
import numpy  as np

app = Flask(__name__)
industry_ai = IndustryAI()
# @app.route("/")
# def hello_world():
#     return "<p> Hello, World! </p>"


#! AI监测端发送命令的路由
@app.route('/questionParse', methods=['POST'])
def question_parse():
    # print('--- i am in question_parse() ---')
    req_obj = request.json
    
    # data = request.json
    # request_body = json.dumps(req_obj)

    question = req_obj.get('question')
    print("\n ========================== 问答 =========================================")
    print('【用户问题】: ',question)

    res = industry_ai.parse(question)  #! 解析主体
    print("\n【大模型返回结果】: ",res['model_res'])
    print("【字典格式结果】: ",res['dict_res'])
    print("【警告】: ",res['warn'])

    # 构建响应
    response_data = {
        'request': req_obj,
        'result': res,
    }
    # return "songfengkun"
    return jsonify(response_data)

    
def trans_base(str):
    bytes = base64.b64decode(str)
    np_array = np.frombuffer(bytes, np.uint8)
    img = cv2.imdecode(np_array,cv2.IMREAD_COLOR)

    return img


if __name__ == '__main__':
    # app.run(debug=True)
    app.run(host='0.0.0.0',port=9191)

